package org.pizza.context;

import java.util.Optional;

/**
 * @author: fyh
 * @date: 2022/4/5
 */
public abstract class BaseContext {

    private static final ThreadLocal<Long> CURRENT_OPERATOR = new ThreadLocal();
    private static final ThreadLocal<Long> CURRENT_TENANT = new ThreadLocal();

    public BaseContext() {
    }

    public static Long operator() {
        return (Long) Optional.ofNullable(CURRENT_OPERATOR.get()).orElse(1L);
    }

    public static Long tenant() {
        return (Long)Optional.ofNullable(CURRENT_TENANT.get()).orElse(1L);
    }

    public static void operatorOf(Long id) {
        CURRENT_OPERATOR.set(id);
    }

    public static void tenantOf(Long id) {
        CURRENT_TENANT.set(id);
    }

    protected static void clearAll() {
        CURRENT_OPERATOR.remove();
        CURRENT_TENANT.remove();
    }
}
